#![allow(dead_code)]
use crate::domain::CountNum;
use crate::domain::GoodsBean;
use crate::domain::GoodsQueryRequest;
use crate::domain::GoodsTypeBean;
use crate::serviceimpl::init_db;
use crate::utils::string_util::is_digit;
use crate::utils::string_util::right_find_under_line;
use crate::serviceimpl::recommend_service::is_scroll as service_is_scroll;
use crate::serviceimpl::recommend_service::is_hot as service_is_hot;
use crate::serviceimpl::recommend_service::is_new as service_is_new;
use sqlx::Error;
use substring::Substring;

pub async fn activate_all_goodses(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let goodses = &app_state.await.context.goodses;
	let vecter_iterator = ids.iter();
	for goods_id in vecter_iterator {
		let _ = goodses.activate_goods(*goods_id).await;
	}
	Ok(0)
}

pub async fn activate_goods(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.activate_goods(id).await
}

pub async fn add_goods(goods:GoodsBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.add_goods(goods).await
}

pub async fn clone_all_goodses(ids:Vec<i64>) ->Result<u64, Error> {
	let vecter_iterator = ids.iter();
	for goods_id in vecter_iterator {
		let _ = clone_goods(*goods_id).await;
	}
	Ok(0)
}

pub async fn clone_goods(id:i64) ->Result<u64, Error> {
	let mut goods = find_goods_by_id(id).await.unwrap();
	let name = find_goods_indexed_name(goods.name).await.unwrap();
	goods.name = name;
	add_goods(goods).await
}

pub async fn count_search_goodses_by_fields_records(goods_query_request:GoodsQueryRequest) ->Result<CountNum, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.count_search_goodses_by_fields_records(goods_query_request).await
}

pub async fn delete_all_goodses(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let goodses = &app_state.await.context.goodses;
	let vecter_iterator = ids.iter();
	for goods_id in vecter_iterator {
		let _ = goodses.delete_goods(*goods_id).await;
	}
	Ok(0)
}

pub async fn delete_goods(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.delete_goods(id).await
}

pub async fn find_goods_by_id(id:i64) ->Result<GoodsBean, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.find_goods_by_id(id).await
}

pub async fn find_goods_by_name(name:String) ->Result<GoodsBean, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.find_goods_by_name(name).await
}

pub async fn list_active_goodses() ->Result<Vec<GoodsBean>, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.list_active_goodses().await
}

pub async fn list_all_goodses() ->Result<Vec<GoodsBean>, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.list_all_goodses().await
}

pub async fn search_goodses_by_fields(goods_query_request:GoodsQueryRequest) ->Result<Vec<GoodsBean>, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.search_goodses_by_fields(goods_query_request).await
}

pub async fn search_goodses_by_fields_by_page(goods_query_request:GoodsQueryRequest) ->Result<Vec<GoodsBean>, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.search_goodses_by_fields_by_page(goods_query_request).await
}

pub async fn soft_delete_all_goodses(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let goodses = &app_state.await.context.goodses;
	let vecter_iterator = ids.iter();
	for goods_id in vecter_iterator {
		let _ = goodses.soft_delete_goods(*goods_id).await;
	}
	Ok(0)
}

pub async fn soft_delete_goods(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.soft_delete_goods(id).await
}

pub async fn toggle_goods(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.toggle_goods(id).await
}

pub async fn toggle_one_goods(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	let goodses = &app_state.await.context.goodses;
	let goods = goodses.find_goods_by_id(id).await.unwrap();
	if goods.active == false {
		let _ = goodses.toggle_goods(id).await;
	} else {
		let count = goodses.count_active_goods_records().await.unwrap();
		if count.count_num > 1 {
			let _ = goodses.toggle_goods(id).await;
		}
	}
	Ok(0)
}

pub async fn update_goods(goods:GoodsBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.goodses.update_goods(goods).await
}

pub async fn find_goods_indexed_name(name:String) ->Result<String, Error> {
	let mut short_name = name.clone();
	if name.contains("_") && is_digit(name.substring(right_find_under_line(name.clone())+1,name.chars().count()).to_string()) {
		short_name = name.substring(0,right_find_under_line(name.clone())).to_string();
	}

	let app_state = init_db();
	let goodses = app_state.await.context.goodses.find_goods_indexed_name(short_name.clone()).await.unwrap();

	let mut index_num = 1;
	for goods in goodses {
		let name2 = goods.name;
		if name2.contains("_") && is_digit(name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).to_string()) {
			let index = name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).parse::<i32>().unwrap();
			if index >= index_num {
				index_num = index + 1;
			}
		}
	}

	let index_name = short_name.clone() + "_" + &index_num.to_string();
	Ok(index_name)
}

pub async fn select_goods_by_recommend(recommend_type_id:i64,page_number:i32) -> Result<Vec<GoodsBean>, Error> {
    let app_state = init_db();
	let mut goodses = app_state.await.context.goodses.select_goods_by_recommend(recommend_type_id,page_number,8).await.unwrap();
	for goods in &mut goodses.iter_mut() {
	    goods.is_scroll = service_is_scroll(goods.id).await.unwrap();
		goods.is_hot = service_is_hot(goods.id).await.unwrap();
		goods.is_new = service_is_new(goods.id).await.unwrap();
	}
	Ok(goodses)
}

pub async fn get_scroll_goodses() -> Result<Vec<GoodsBean>, Error> {
    let app_state = init_db();
	app_state.await.context.goodses.get_scroll_goodses().await
}

pub async fn get_goods_list(recommendType:i64) -> Result<Vec<GoodsBean>, Error> {
    let app_state = init_db();
	app_state.await.context.goodses.get_goods_list(recommendType).await
}

pub async fn select_goods_by_goods_type_id(goods_type_id:i64,page_number:i32,page_size:i32) -> Result<Vec<GoodsBean>, Error> {
    let app_state = init_db();
	app_state.await.context.goodses.select_goods_by_goods_type_id(goods_type_id,page_number,page_size).await
}

pub async fn count_active_goodses_by_goods_type_id(goods_type_id:i64) -> Result<CountNum, Error> {
    let app_state = init_db();
	app_state.await.context.goodses.count_active_goodses_by_goods_type_id(goods_type_id).await
}

pub async fn count_active_goodses_by_recommend_type_id(recommend_type_id:i64) -> Result<CountNum, Error> {
    let app_state = init_db();
	app_state.await.context.goodses.count_active_goodses_by_recommend_type_id(recommend_type_id).await
}

pub async fn search_goods_by_page(keyword:String,page_number:i32,page_size:i32) -> Result<Vec<GoodsBean>, Error> {
    let app_state = init_db();
	app_state.await.context.goodses.search_goods_by_page(keyword,page_number,page_size).await
}
